package com.zxmr.utility.action;

import java.io.UnsupportedEncodingException;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.zxmr.action.Request;
import com.zxmr.action.Response;
import com.zxmr.action.ResponsePayload;
import com.zxmr.action.ResultCode;
import com.zxmr.domain.Report;
import com.zxmr.domain.service.ReportSearchBean;
import com.zxmr.domain.service.ReportService;
import com.zxmr.navigate.ExecuteException;
import com.zxmr.navigate.Executor;
import com.zxmr.util.ResourceManager;
import com.zxmr.util.Utils;
import com.zxmr.utility.dto.JSONListDTO;
import com.zxmr.utility.service.UserActionTraceService;

public class ReportAction extends Executor{

	enum method{
		add,
		queryAll,
		query;
	}
	@Override
	public void execute(Request request, Response response) {
		
		try
		{
			method m = Enum.valueOf(method.class, this.getElement().getMethod());
			switch(m)
			{
			case add:
				add(request,response);
				break;
			case query:
				query(request,response);
				break;
			case queryAll:
				queryAll(request,response);
				break;
			}

		}catch(Exception e)
		{
			ResponsePayload payload = new ResponsePayload();
			if(e instanceof ExecuteException)
			{
				payload.setValue(e.getMessage());
			}else
			{
				e.printStackTrace();
				payload.setValue("internal error");
			}
			response.setPayload(payload);
			response.setResult(ResultCode.COMPONENT_ERROR);
		}
	}
	
	private void add(Request request,Response response)throws ExecuteException
	{
		Report report = parseRequest(request);
		report.setOwner(request.getAccount().getName());
		report.setCompany(request.getAccount().getCompany());
		report.setCreateDate(Utils.getCurrentTime());
		
		try {
			report.setContent(new String(report.getContent().getBytes("UTF-8")));
		} catch (UnsupportedEncodingException e) {
			throw new ExecuteException("internal error");
		}
		ReportService service = new ReportService();
		int id = service.add(report);
		if(id == -1)this.internalErrorResponse(response);
		
		report.setId(id);
		ResponsePayload payload = new ResponsePayload(JSON.toJSONString(report));
		response.setPayload(payload);
		
		UserActionTraceService userTraceService = new UserActionTraceService();
		String action  = ResourceManager.getInstance().getMessage("com.zxmr.utility.addReport", String.valueOf(report.getId()),report.getTitle());
		userTraceService.addActionTrace(request.getAccount(), action);
	}
	private Report parseRequest(Request request)throws ExecuteException
	{
		try
		{
			return JSON.parseObject(request.getRequestParameters().get("parameter"), Report.class);
		}catch(Exception e)
		{
			throw new ExecuteException("not valid parameter");
		}
	}
	private void query(Request request,Response response)throws ExecuteException
	{
		int id = 0;
		try
		{
			id = Integer.parseInt(request.getRequestParameters().get("id"));
		}catch(Exception e)
		{
			throw new ExecuteException("id parameter is not valid");
		}
		
		ReportService service = new ReportService();
		Report report = service.query(id);
		if(report == null || report.getCompany() != request.getAccount().getCompany() 
				|| !request.getAccount().getName().equals(report.getOwner()))
			throw new ExecuteException("you are not authorized");
		
		ResponsePayload payload = new ResponsePayload(JSON.toJSONString(report));
		response.setPayload(payload);
	}
	
	private void queryAll(Request request,Response response)throws ExecuteException
	{
		int start = getStart(request);
		int limit = getLimit(request);
		
		ReportSearchBean searchBean = new ReportSearchBean();
		searchBean.setCompany(request.getAccount().getCompany());
		searchBean.setOwner(request.getAccount().getName());
		searchBean.setLimit(limit);
		searchBean.setStart(start);
		
		ReportService service = new ReportService();
		List<Report> report = service.queryContractPagination(searchBean);
		int count = service.queryContractPaginationCount(searchBean);
		
		JSONListDTO dto = new JSONListDTO();
		dto.setRows(report);
		dto.setTotal(count);
		
		ResponsePayload payload = new ResponsePayload(JSON.toJSONString(dto));
		response.setPayload(payload);
	}
	private int getStart(Request request)throws ExecuteException
	{
		try
		{
			return Integer.parseInt(request.getRequestParameters().get("start"));
		}catch(Exception e)
		{
			throw new ExecuteException("start parameter is not valid");
		}
	}
	
	private int getLimit(Request request)throws ExecuteException
	{
		try
		{
			return Integer.parseInt(request.getRequestParameters().get("limit"));
		}catch(Exception e)
		{
			throw new ExecuteException("limit parameter is not valid");
		}
	}
}
